<template>
  <div
      :style='{"width":"1200px","padding":"120px 7% 100px","margin":"0px auto","position":"relative","background":"url(http://codegen.caihongy.cn/20230109/138a4d606eae4a168810cef229969885.png) no-repeat center top,url(http://codegen.caihongy.cn/20230109/b95aeae1c1294f398aefb7c6cd5b16de.png) no-repeat center top,url(http://codegen.caihongy.cn/20230109/5ffc803e6682418eb7f0b09a98e35527.png) no-repeat center bottom,#99CC99"}'>
    <div class="section-title"
         :style='{"margin":"10px 0","color":"#333","textAlign":"center","background":"none","width":"100%","fontSize":"22px","lineHeight":"54px","fontWeight":"600"}'>
      论坛交流
    </div>
    <el-form class="add-update-preview" :model="form" :rules="rules" ref="form" label-width="150px">
      <el-form-item :style='{"padding":"10px","boxShadow":"0 2px 0 #efe4d4","margin":"0 0 10px"}' label="标题"
                    prop="title">
        <el-input v-model="form.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item :style='{"padding":"10px","boxShadow":"0 2px 0 #efe4d4","margin":"0 0 10px"}' label="类型"
                    prop="isdone">
        <el-radio-group v-model="form.isdone">
          <el-radio label="开放">公开</el-radio>
          <el-radio label="关闭">私人</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :style='{"padding":"10px","boxShadow":"0 2px 0 #efe4d4","margin":"0 0 10px"}' label="内容"
                    prop="content">
        <quill-editor ref="myTextEditor"
                      v-model="form.content"
                      :config="editorOption"
                      @ready="onEditorReady($event)">
        </quill-editor>
      </el-form-item>
      <el-form-item :style='{"padding":"0","textAlign":"center","margin":"0"}'>
        <el-button
            :style='{"border":"0","cursor":"pointer","padding":"0","margin":"0 20px 0 0","outline":"none","color":"rgba(255, 255, 255, 1)","borderRadius":"4px","background":"#ff9966","width":"128px","lineHeight":"40px","fontSize":"14px","height":"40px"}'
            type="primary" @click="submitForm('form')">{{ this.isEdit ? '修改' : '发布帖子' }}
        </el-button>
        <el-button
            :style='{"border":"0px solid rgba(64, 158, 255, 1)","cursor":"pointer","padding":"0","margin":"0","outline":"none","color":"#fff","borderRadius":"4px","background":"#009688","width":"128px","lineHeight":"40px","fontSize":"14px","height":"40px"}'
            @click="resetForm('form')">重置
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  //数据集合
  data() {
    return {
      form: {
        title: '',
        isdone: '开放',
        content: '',
        parentid: 0,
        userid: localStorage.getItem('userid'),
        username: localStorage.getItem('username')
      },
      editorOption: {
        modules: {
          toolbar: [
            ["bold", "italic", "underline", "strike"],
            ["blockquote", "code-block"],
            [{header: 1}, {header: 2}],
            [{list: "ordered"}, {list: "bullet"}],
            [{script: "sub"}, {script: "super"}],
            [{indent: "-1"}, {indent: "+1"}],
            [{direction: "rtl"}],
            [{size: ["small", false, "large", "huge"]}],
            [{header: [1, 2, 3, 4, 5, 6, false]}],
            [{color: []}, {background: []}],
            [{font: []}],
            [{align: []}],
            ["clean"],
            ["image", "video"]
          ]
        }
      },
      isEdit: false,
      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    if (this.$route.query.id != undefined) {
      this.isEdit = true;
      this.form = Object.assign({}, JSON.parse(this.$route.query.editObj));
    }
  },
  //方法集合
  methods: {
    onEditorReady(editor) {
      editor.root.setAttribute('data-placeholder', "请输入内容...");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post(`forum/${this.isEdit ? 'update' : 'add'}`, this.form).then(res => {
            if (res.data.code === 0) {
              this.$message({
                message: `${this.isEdit ? '修改' : '发布'}成功`,
                type: 'success',
                duration: 1500,
                onClose: () => {
                  if (this.isEdit) {
                    this.$router.push('/index/myForumList');
                  } else {
                    this.$router.push('/index/forum');
                  }
                }
              });
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.add-update-preview .el-form-item ::v-deep .el-form-item__label {
  padding: 0 10px 0 0;
  color: #666;
  font-weight: 500;
  width: 150px;
  font-size: 14px;
  line-height: 40px;
  text-align: right;
}

.add-update-preview .el-form-item ::v-deep .el-form-item__content {
  margin-left: 150px;
}

.add-update-preview .el-input ::v-deep .el-input__inner {
  border: 1px solid #e5d9c6;
  border-radius: 4px;
  padding: 0 12px;
  outline: none;
  color: #333;
  width: 400px;
  font-size: 14px;
  height: 40px;
}

.add-update-preview .el-select ::v-deep .el-input__inner {
  border: 1px solid #e5d9c6;
  border-radius: 4px;
  padding: 0 10px;
  outline: none;
  color: #333;
  width: 200px;
  font-size: 14px;
  height: 40px;
}

.add-update-preview .el-date-editor ::v-deep .el-input__inner {
  border: 1px solid #e5d9c6;
  border-radius: 4px;
  padding: 0 10px 0 30px;
  outline: none;
  color: #333;
  width: 200px;
  font-size: 14px;
  height: 40px;
}

.add-update-preview ::v-deep .el-upload--picture-card {
  background: transparent;
  border: 0;
  border-radius: 0;
  width: auto;
  height: auto;
  line-height: initial;
  vertical-align: middle;
}

.add-update-preview ::v-deep .upload .upload-img {
  border: 1px solid #e5d9c6;
  cursor: pointer;
  border-radius: 6px;
  color: #333;
  width: 100px;
  font-size: 32px;
  line-height: 100px;
  text-align: center;
  height: 100px;
}

.add-update-preview ::v-deep .el-upload-list .el-upload-list__item {
  border: 1px solid #e5d9c6;
  cursor: pointer;
  border-radius: 6px;
  color: #333;
  width: 100px;
  font-size: 32px;
  line-height: 100px;
  text-align: center;
  height: 100px;
}

.add-update-preview ::v-deep .el-upload .el-icon-plus {
  border: 1px solid #e5d9c6;
  cursor: pointer;
  border-radius: 6px;
  color: #333;
  width: 100px;
  font-size: 32px;
  line-height: 100px;
  text-align: center;
  height: 100px;
}

.add-update-preview .el-textarea ::v-deep .el-textarea__inner {
  border: 1px solid #e5d9c6;
  border-radius: 4px;
  padding: 12px;
  outline: none;
  color: #333;
  width: 400px;
  font-size: 14px;
  height: 120px;
      }
</style>
